package com.are.odisea;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class ManejadorImagenNoticia {
	private db conexion = null;
	private ImagenNoticia imgnot;
	
	public static final int ORDER_ID = 1;
	public static final int ORDER_NOT = 2;
	public static final int ORDER_CAT = 3;

	public ManejadorImagenNoticia(db conexion) {
		super();
		this.setConexion(conexion);
		this.setImgnot(new ImagenNoticia());
	}
	public ManejadorImagenNoticia(db conexion, ImagenNoticia imgnot) {
		super();
		this.setConexion(conexion);
		this.setImgnot(imgnot);
	}
	public db getConexion() {
		return conexion;
	}
	public void setConexion(db conexion) {
		this.conexion = conexion;
	}
	public ImagenNoticia getImgnot() {
		return imgnot;
	}
	public void setImgnot(ImagenNoticia imgnot) {
		this.imgnot = imgnot;
	}
	
	public boolean add() throws SQLException{
		boolean result = false;

			String sql = "insert into Imagen_Noticia (ruta,titulo,pie_foto,noticia) values (?,?,?,?)";
			java.sql.PreparedStatement pst = conexion.getConnection().prepareStatement(sql);
			pst.setString(1, imgnot.getRuta());
			pst.setString(2, imgnot.getTitulo());
			pst.setString(3, imgnot.getPie_foto());
			pst.setString(4, imgnot.getNoticia());
			if (conexion.Update(pst) > 0) {
				conexion.Commit();
				result = true;
			}
		return result;
	}
	
	public boolean update(String key) throws SQLException{
		boolean result = false;

			String sql = "update imagen_noticia set titulo= ?, ruta=?,pie_foto=?,noticia=? where id=?";
			
			java.sql.PreparedStatement pst = conexion.getConnection().prepareStatement(sql);
			pst.setString(1, imgnot.getRuta());
			pst.setString(2, imgnot.getTitulo());
			pst.setString(3, imgnot.getPie_foto());
			pst.setString(4, imgnot.getNoticia());
			pst.setString(5, key);
			
			if (conexion.Update(pst) > 0) {
				conexion.Commit();
				result = true;
			}
			
		return result;
	}
	
	public boolean remove(String key) throws SQLException {
		boolean result = false;

			String sql = "delete from imagen_noticia where id=?";
			java.sql.PreparedStatement pst = conexion.getConnection().prepareStatement(sql);
			pst.setString(1,key);
			if (conexion.Update(pst) > 0) {
				conexion.Commit();
				result = true;
			}
			

		return result;	
	}
	
	
	public boolean exist(String key) throws SQLException {
		boolean result = false;

			String sql = "select * from imagen_noticia where id = ?";
			java.sql.PreparedStatement pst = conexion.getConnection().prepareStatement(sql);
			pst.setString(1,key);
			ResultSet rs = conexion.Query(pst);
			if (rs.next()) {
				result = true;
			}
			rs.close();
			
		
		return result;	
	}
	
	public boolean Find(String key) throws SQLException {
		boolean result = false;

			String sql = "select * from imagen_noticia where id=?";
			java.sql.PreparedStatement pst = conexion.getConnection().prepareStatement(sql);
			pst.setString(1,key);
			ResultSet rs = conexion.Query(pst);
			if (rs.next()) {
				imgnot = new ImagenNoticia();
				imgnot.setTitulo(rs.getString("titulo"));
				imgnot.setRuta(rs.getString("ruta"));
				imgnot.setNoticia(rs.getString("noticia"));
				imgnot.setPie_foto(rs.getString("pie_foto"));
				imgnot.setId(rs.getString("id"));
				result = true;
			}
			rs.close();
		return result;	
	}
	
	public ArrayList<ImagenNoticia> list() throws SQLException {
		ArrayList<ImagenNoticia> notis = new ArrayList<ImagenNoticia>();
		
			String sql = "select * from vnoticias order by id";
			ResultSet rs  = conexion.Query(sql);
			while(rs.next()) {
				ImagenNoticia in = new ImagenNoticia();
				in.setTitulo(rs.getString("titulo"));
				in.setRuta(rs.getString("ruta"));
				in.setNoticia(rs.getString("noticia"));
				in.setPie_foto(rs.getString("pie_foto"));
				in.setId(rs.getString("id"));
				notis.add(in);
			}
			rs.close();
		return notis;
	}
	
	public ArrayList<ImagenNoticia> list(int orden) throws SQLException {
		ArrayList<ImagenNoticia> notis = new ArrayList<ImagenNoticia>();

			String sql = "select * from imagen_noticia";
			
			switch(orden) {
			case ORDER_ID:
				sql += " order by id";
			break;
			case ORDER_NOT:
				sql += " order by noticia desc";
			break;
			}
			ResultSet rs  = conexion.Query(sql);
			while(rs.next()) {
				ImagenNoticia in = new ImagenNoticia();
				in.setTitulo(rs.getString("titulo"));
				in.setRuta(rs.getString("ruta"));
				in.setNoticia(rs.getString("noticia"));
				in.setPie_foto(rs.getString("pie_foto"));
				in.setId(rs.getString("id"));
				notis.add(in);
			}
			rs.close();
		return notis;	
	}
	
	public ArrayList<ImagenNoticia> listImgNot(String not, int orden) throws SQLException {
		ArrayList<ImagenNoticia> notis = new ArrayList<ImagenNoticia>();

			String sql = "select * from imagen_noticia where noticia='"+not+"'";
			
			switch(orden) {
			case ORDER_ID:
				sql += " order by id";
			break;
			case ORDER_NOT:
				sql += " order by noticia desc";
			break;
			}
			ResultSet rs  = conexion.Query(sql);
			while(rs.next()) {
				ImagenNoticia in = new ImagenNoticia();
				in.setTitulo(rs.getString("titulo"));
				in.setRuta(rs.getString("ruta"));
				in.setNoticia(rs.getString("noticia"));
				in.setPie_foto(rs.getString("pie_foto"));
				in.setId(rs.getString("id"));
				notis.add(in);
			}
			rs.close();
		return notis;	
	}
}
